package classes
{
	[Bindable]
	public class Transaction implements WritableObject
	{
		
		public var description:String = "";
		public var isReconciled:Boolean = false;
		public var isApproved:Boolean = false;
		public var accountName:String = "";

		private var _date:Date;
		private var _isDeposit:Boolean = true;
		private var _amount:Number = 0;
		private var _recipient:Recipient = new Recipient();
		private var _category:Category = new Category();
		private var _isScheduled:Boolean = false;
		
		
		public function Transaction(){
			
		}
		
		public function parseTransactionString(raw:XML):void{
			this.amount = raw.@amount;
			this.category = raw.@category;
			this.date = new Date(raw.@date);
			this.description = raw.@description;
			this.isReconciled = raw.@isReconciled.toUpperCase() == "TRUE";
			this.isScheduled = raw.@isScheduled.toUpperCase() == "TRUE";
			this.isDeposit = raw.@isDeposit.toUpperCase() == "TRUE";
			this.recipient = raw.@recipient;
		}
		
		public function toWritableString():String
		{
			var s:String = "<Transaction date=\"" + this.date.time + "\" amount=\"" + this.amount +
							"\" recipient=\"" + this.recipient +  
							"\" category=\"" + this.category + 
							"\" description=\"" + this.description +
							"\" isDeposit=\"" + this.isDeposit +
							"\" isScheduled=\"" + this.isScheduled + 
							"\" isReconciled=\"" + this.isReconciled +  "\" />";
			
			return s;
			//return 
		}
		
		public function clone():Transaction{
			var t:Transaction = new Transaction();
			for(var i:String in this){
				t[i] == this[i];
			}
			return t;
		}
		
		//determines if this transaction is scheduled or not, and returns.
		public function get isScheduled():Boolean{
			/* var scheduled:Boolean = (this.date.time > new Date().time);
			
			/* moved to account.as
			if(this._isScheduled && !scheduled){
				//TODO: ADD TRANSACTION TO APPROVAL LIST
				//	This will also need to include the account 
				//	to which it wll be applied
			}
			this._isScheduled = scheduled; */
			
			
			return this._isScheduled;
		}
		
		public function set isScheduled(sched:Boolean):void{
			this._isScheduled = sched;
		}
		
		public function get category():String{
			return this._category.name;
		}
		
		public function set category(key:String):void{
			this._category = TreasuryModel.getInstance().getCategory(key);
		}
		
		public function get recipient():String{
			return this._recipient.name;
		}
		
		public function set recipient(key:String):void{
			this._recipient = TreasuryModel.getInstance().getRecipient(key);
		}
		
		public function get isDeposit():Boolean{
			return this._isDeposit;
		}
		
		public function set isDeposit(d:Boolean):void{
			if(this._isDeposit != d){
				this._isDeposit = d;
				TreasuryModel.getInstance().recalculateUserData();
			}
			this._isDeposit = d;
		}
		
		public function get amount():Number{
			return (this.isDeposit ? 1 : (-1)) * this._amount;
		}
		
		public function set amount(n:Number):void{
			if(!isNaN(n)){
				//always add positive number
				this._amount = (n < 0 ? (-1)*n : n);
				this.isReconciled = false;
				TreasuryModel.getInstance().recalculateUserData();
			}
		}
		public function get date():Date{
			return this._date;
		}
		public function set date(t:Date):void{
			this._date = t;
			if(this._date.time > new Date().time){
				this._isScheduled = true;
			}
		}

	}
}